/*====================================================================================
    EVS Codec 3GPP TS26.443 Nov 13, 2018. Version 12.11.0 / 13.7.0 / 14.3.0 / 15.1.0
  ====================================================================================*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <assert.h>
#include "options.h"
#include "prot.h"
#include "cnst.h"
#include "rom_com.h"
#include "g192.h"

/*------------------------------------------------------------------------------------------*
 * Global variables
 *------------------------------------------------------------------------------------------*/
long dec_frame = 0;                 /* Counter of frames */




void LibEVS_reset_indices_dec(void *st)
{
    Decoder_State *pst = (Decoder_State*)st;
    reset_indices_dec(pst);
}

void LibEVS_init_decoder(void *st)
{
    Decoder_State *pst = (Decoder_State*)st;
    init_decoder(pst);
}

void LibEVS_destroy_decoder(void *st)
{
    Decoder_State *pst = (Decoder_State*)st;
    destroy_decoder(pst);
}

void LibEVS_dec_free_params(void **st)
{
    Decoder_State *pst = (Decoder_State*)(*st);
    free(pst);
    *st = NULL;
}

int LibEVS_is_AMR_WB(void *st)
{
	Decoder_State *pst = (Decoder_State*)st;
	return pst->Opt_AMR_WB==1 ? 1 : 0;
}

void LibEVS_amr_wb_dec
(
    void *st,                      /* i/o: decoder state structure         */
    float *output_sp                /* o  : synthesis output                */
)
{
    Decoder_State *pst = (Decoder_State*)st;
    amr_wb_dec(pst, output_sp);
}

void LibEVS_evs_dec
(
    void *st,                       /* i/o: Decoder state structure                 */
    float *output,                   /* o  : output synthesis signal                 */
    frameMode frameMode                  /* i  : Decoder frame mode                      */
)
{
    Decoder_State *pst = (Decoder_State*)st;
    evs_dec(pst, output, frameMode);
}

unsigned int LibEVS_syn_output(float *synth, const short output_frame,short *synth_out)
{
    return syn_output(synth, output_frame, synth_out);
}

void LibEVS_increase_Frames(void *pst)
{
    Decoder_State *st = (Decoder_State*)pst;
    if (st->ini_frame < MAX_FRAME_COUNTER)
    {
        st->ini_frame++;
    }
}

int LibEVS_dec_init_params(int sampling,void **st)
{
    if ((*st = (Decoder_State *)malloc(sizeof(Decoder_State))) == NULL)
    {
        fprintf(stderr, "Can not allocate memory for decoder state structure\n");
        return(-1);
    }
    //Decoder_State *pst = (Decoder_State*)(*st);
    dec_init_params(sampling, *st);

    srand((unsigned int)time(0));

    return 0;
}

Word16 LibEVS_read_indices_mime_head
(                /* o  : 1 = reading OK, 0 = problem            */
    void *st,                   /* i/o: decoder state structure                */
    UWord8 *pFrame,//FILE *file,          /* i  : bitstream file                         */
    Word16 rew_flag                      /* i  : rewind flag (rewind file after reading)*/
)
{
    Decoder_State *pst = (Decoder_State*)st;
    return read_indices_mime_head(pst, pFrame, rew_flag);
}

Word16 LibEVS_read_indices_mime_buf
(                /* o  : 1 = reading OK, 0 = problem            */
    void *st,                   /* i/o: decoder state structure                */
    UWord8 *header,
    UWord8 *pFrame,//FILE *file,          /* i  : bitstream file                         */
    Word16 rew_flag                      /* i  : rewind flag (rewind file after reading)*/
)
{
    Decoder_State *pst = (Decoder_State*)st;
    return read_indices_mime_buf(pst, header, pFrame, rew_flag);
}

int LibEVS_is_CODEC_MODE1(void *st)
{
    Decoder_State *pst = (Decoder_State*)st;
    return pst->codec_mode == MODE1 ? 1 : 0;
}

int LibEVS_is_BFI(void *st)
{
    Decoder_State *pst = (Decoder_State*)st;
    return pst->bfi;
}
